package com.sojson.util.db.compare.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sojson.util.db.compare.bean.ColumnInfo;

/**
 * 数据库比较执行器
 * 
 * @author liu
 * @date 2022-11-21
 */
public class CompareTable {

    /**
     * 执行方法
     * 
     * @param args
     */
    public static void main(String[] args) {
        DbCompareUtil.initData();
        compareData();
        System.out.println("end------------------");
    }

    public static void compareData() {
        if (DbCompareUtil.map_144.isEmpty()) {
            return;
        }
        List<String> tableNames = new ArrayList<>();
        for (String key : DbCompareUtil.map_144.keySet()) {
            if (DbCompareUtil.map_143.containsKey(key)) {
                compareTable(DbCompareUtil.map_143.get(key), DbCompareUtil.map_144.get(key), key);
            } else {
                tableNames.add(key);
            }
        }
        if (tableNames.size() > 0) {
            StringBuilder sb = new StringBuilder(tableNames.size());
            for (String s : tableNames) {
                sb.append(s + ",");
            }
            System.out.println("============================================");
            System.out.println("============================================");
            System.out.println("============================================");
            System.out.println("error: ");
            System.out.println("143上相对于144上缺少的表: " + sb.toString().replace(",", System.lineSeparator()));
        }
    }

    public static boolean compareTable(Map<String, ColumnInfo> map143, Map<String, ColumnInfo> map144,
        String tableName) {
        if (map143.isEmpty() || map144.isEmpty()) {
            return false;
        }
        // 同一张表上 143上相对于144上缺少的字段
        List<String> lackColumns = new ArrayList<>();
        // 同一张表上相同字段 143上相对于144上有差异的
        // List<ColumnInfo> diffColumns = new ArrayList<ColumnInfo>();
        for (String key : map144.keySet()) {
            if (map143.containsKey(key)) {
                if (!compareColumn(map143.get(key), map144.get(key))) {
                    System.out.println("============================================");
                    System.out.println("============================================");
                    System.out.println("============================================");
                    System.out.println("error: 表名:" + tableName);
                    System.out.println("key---" + key);
                    System.out.println("143----" + map143.get(key).toString());
                    System.out.println("144----" + map144.get(key).toString());
                }
            } else {
                lackColumns.add(key);
            }
        }
        if (lackColumns.size() > 0) {
            StringBuilder sb = new StringBuilder(lackColumns.size());
            for (String s : lackColumns) {
                sb.append(s + ",");
            }
            System.out.println("============================================");
            System.out.println("============================================");
            System.out.println("============================================");
            System.out.println("error: 表名:" + tableName);
            System.out
                .println("表" + tableName + "在143上相对于144上缺少的字段: " + sb.toString().replace(",", System.lineSeparator()));
        }
        return true;
    }

    public static boolean compareColumn(ColumnInfo info143, ColumnInfo info144) {
        if (info143 == null || info144 == null) {
            return false;
        }
        if (!info143.getTableName().equalsIgnoreCase(info144.getTableName())) {
            return false;
        }
        if (!info143.getColumnName().equalsIgnoreCase(info144.getColumnName())) {
            return false;
        }
        if (!info143.getColumnType().equalsIgnoreCase(info144.getColumnType())) {
            return false;
        }
        if (Long.parseLong(info143.getColumnLen()) < Long.parseLong(info144.getColumnLen())) {
            return false;
        }
        if (!info143.getColumnkey().equalsIgnoreCase(info144.getColumnkey())) {
            return false;
        }
        if (!info143.getColumnExtra().equalsIgnoreCase(info144.getColumnExtra())) {
            return false;
        }
        // if(!info1.getColumnComment().equalsIgnoreCase(info2.getColumnComment()))return false;
        if (info143.getIsNullAble().equals("NO") && info144.getIsNullAble().equals("YES")) {
            return false;
        }
        // if(!info1.getColumnDefault().equalsIgnoreCase(info2.getColumnDefault()))return false;
        return true;
    }

}